<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */


class Core_Form_User_Edit extends RedCross_Form
{
    public function init()
    {
  $this->addElement('text', 'address', array(
      'filters'    => array('StringTrim'),
            'validators'    =>  array(
                array('Alnum', true, true),
                array('StringLength', true, array(4,32)),
            ),
            'label'   => 'Adresse'
  ));
  $this->addElement('text', 'address_2', array(
      'filters'    => array('StringTrim'),
            'validators'    =>  array(
                array('Alnum', true, true),
                array('StringLength', true, array(4,32)),
            ),
            'label'   => 'Adresse'
  ));

        $this->addElement('select', 'postal_code', array(
      'filters'    => array('StringTrim'),
      'validators'  => array(
                array('NotEmpty', true),
        array('Digits', true),
            ),
            'label' => 'Postnummer'
        ));

        $this->getElement('postal_code')->addMultiOption(null, '-- Velg postnummer --');

  $this->addElement('hidden', 'submit_personalia');
  $this->addElement('submit', 'submit', array(
            'required' => true,
            'ignore'   => true,
            'label'    => 'Lagre',
        ));
       //$this->getElement(Admin_MemberController::SUBMIT_PERSONALIA)->setDecorators($this->_buttonElementDecorators);

        $this->fillPostalCodes();
  }

    private function fillPostalCodes()
    {
        $postalService = RedCross_Factory::getService('Core_Service_Postal');
        $postalCodes = $postalService->fetchAll();
        $postalElement = $this->getElement('postal_code');
        foreach($postalCodes as $postal) {
            $postalElement->addMultiOption($postal['id'], $this->getPostalCode($postal['id']) . ' - ' . $postal['area']);
        }
    }

    private function getPostalCode($postal_id)
    {
        $length = strlen($postal_id);
        switch($length) {
            case 1:
                return '000' . $postal_id;
                break;
            case 2:
                return '00' . $postal_id;
                break;
            case 3:
                return '0' . $postal_id;
                break;
        }
        return $postal_id;
    }

    public function fillForm(Core_Model_Member $member)
    {
       // $this->getElement('first_name')->setValue($member->first_name);
       // $this->getElement('last_name')->setValue($member->last_name);
        $this->getElement('address')->setValue($member->address);
        $this->getElement('address_2')->setValue($member->address_2);
        $this->getElement('postal_code')->setValue($member->getPostalCode());
    }
}

